home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / misc.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  4KB  |  233 lines

  1. /* Miscellaneous machine independent utilities
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  *
  4.  * ATARI Version by David Nash - dnash@chaso.demon.co.uk
  5.  */
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "socket.h"
  9. #include "mbuf.h"
  10.  
  11. /* Select from an array of strings, or return ascii number if out of range */
  12. char *
  13. smsg(msgs,nmsgs,n)
  14. char *msgs[];
  15. unsigned nmsgs,n;
  16. {
  17.     static char buf[16];
  18.  
  19.     if(n < nmsgs && msgs[n] != NULLCHAR)
  20.         return msgs[n];
  21.     sprintf(buf,"%u",n);
  22.     return buf;
  23. }
  24.  
  25. /* Convert hex-ascii to integer */
  26. int
  27. htoi(s)
  28. char *s;
  29. {
  30.     int i = 0;
  31.     char c;
  32.  
  33.     while((c = *s++) != '\0'){
  34.         if(c == 'x')
  35.             continue;    /* allow 0x notation */
  36.         if('0' <= c && c <= '9')
  37.             i = (i * 16) + (c - '0');
  38.         else if('a' <= c && c <= 'f')
  39.             i = (i * 16) + (c - 'a' + 10);
  40.         else if('A' <= c && c <= 'F')
  41.             i = (i * 16) + (c - 'A' + 10);
  42.         else
  43.             break;
  44.     }
  45.     return i;
  46. }
  47. /* replace terminating end of line marker(s) with null */
  48. void
  49. rip(s)
  50. register char *s;
  51. {
  52.     register char *cp;
  53.  
  54.     if((cp = strchr(s,'\n')) != NULLCHAR)
  55.         *cp = '\0';
  56. }
  57.  
  58. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  59.  * library, but it doesn't call mallocw() and can therefore return NULL.
  60.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  61.  */
  62. char *
  63. strdup(s)
  64. const char *s;
  65. {
  66.     register char *out;
  67.     register int len;
  68.  
  69.     if(s == NULLCHAR)
  70.         return NULLCHAR;
  71.     len = strlen(s);
  72.     out = mallocw(len+1);
  73.     /* This is probably a tad faster than strcpy, since we know the len */
  74.     memcpy(out,s,len);
  75.     out[len] = '\0';
  76.     return out;
  77. }
  78. /* Routines not needed for Turbo 2.0, but available for older libraries */
  79. #ifdef    AZTEC
  80.  
  81. /* Case-insensitive string comparison */
  82. strnicmp(a,b,n)
  83. register char *a,*b;
  84. register int n;
  85. {
  86.     char a1,b1;
  87.  
  88.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  89.         if(a1 == b1)
  90.             continue;    /* No need to convert */
  91.         a1 = tolower(a1);
  92.         b1 = tolower(b1);
  93.         if(a1 == b1)
  94.             continue;    /* NOW they match! */
  95.         if(a1 > b1)
  96.             return 1;
  97.         if(a1 < b1)
  98.             return -1;
  99.     }
  100.     return 0;
  101. }
  102.  
  103. char *
  104. strtok(s1,s2)
  105. char *s1;    /* Source string (first call) or NULL */
  106. #ifdef    __STDC__    /* Ugly kludge for aztec's declaration */
  107. const char *s2;    /* Delimiter string */
  108. #else
  109. char *s2;    /* Delimiter string */
  110. #endif
  111. {
  112.     static int isdelim();
  113.     static char *next;
  114.     register char *cp;
  115.     char *tmp;
  116.  
  117.     if(s2 == NULLCHAR)
  118.         return NULLCHAR;    /* Must give delimiter string */
  119.  
  120.     if(s1 != NULLCHAR)
  121.         next = s1;        /* First call */
  122.  
  123.     if(next == NULLCHAR)
  124.         return NULLCHAR;    /* No more */
  125.  
  126.     /* Find beginning of this token */
  127.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  128.         ;
  129.  
  130.     if(*cp == '\0')
  131.         return NULLCHAR;    /* Trailing delimiters, no token */
  132.  
  133.     /* Save the beginning of this token, and find its end */
  134.     tmp = cp;
  135.     next = NULLCHAR;    /* In case we don't find another delim */
  136.     for(;*cp != '\0';cp++){
  137.         if(isdelim(*cp,s2)){
  138.             *cp = '\0';
  139.             next = cp + 1;    /* Next call will begin here */
  140.             break;
  141.         }
  142.     }
  143.     return tmp;
  144. }
  145. static int
  146. isdelim(c,delim)
  147. char c;
  148. register char *delim;
  149. {
  150.     char d;
  151.  
  152.     while((d = *delim++) != '\0'){
  153.         if(c == d)
  154.             return 1;
  155.     }
  156.     return 0;
  157. }
  158. #endif    /* AZTEC */
  159.  
  160.  
  161.  
  162. /* Host-network conversion routines, replaced on the x86 with
  163.  * assembler code in pcgen.asm
  164.  */
  165. #ifndef    MSDOS
  166. /* Put a long in host order into a char array in network order */
  167. char *
  168. put32(cp,x)
  169. register char *cp;
  170. int32 x;
  171. {
  172.     *cp++ = x >> 24;
  173.     *cp++ = x >> 16;
  174.     *cp++ = x >> 8;
  175.     *cp++ = x;
  176.     return cp;
  177. }
  178. /* Put a short in host order into a char array in network order */
  179.  
  180. char *put16(
  181.     char  *cp,
  182.     int16 x )
  183. {
  184.     *cp++ = x >> 8;
  185.     *cp++ = x;
  186.  
  187.     return cp;
  188. }
  189. int16
  190. get16(cp)
  191. register char *cp;
  192. {
  193.     register int16 x;
  194.  
  195.     x = uchar(*cp++);
  196.     x <<= 8;
  197.     x |= uchar(*cp);
  198.     return x;
  199. }
  200. /* Machine-independent, alignment insensitive network-to-host long conversion */
  201. int32
  202. get32(cp)
  203. register char *cp;
  204. {
  205.     int32 rval;
  206.  
  207.     rval = uchar(*cp++);
  208.     rval <<= 8;
  209.     rval |= uchar(*cp++);
  210.     rval <<= 8;
  211.     rval |= uchar(*cp++);
  212.     rval <<= 8;
  213.     rval |= uchar(*cp);
  214.  
  215.     return rval;
  216. }
  217. /* Compute int(log2(x)) */
  218.  
  219. int log2(int16 x)
  220. {
  221.     register int n = 16;
  222.     for(;n != 0;n--){
  223.         if(x & 0x8000)
  224.             break;
  225.         x <<= 1;
  226.     }
  227.     n--;
  228.     return n;
  229. }
  230.  
  231. #endif
  232.  
  233.